package leetcode.easy.q125;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Q {
    public static void main(String[] args) {
        log.info("a:{},A:{},0:{}", (int) 'a', (int) 'A', (int) '0');
        log.info("z:{},Z:{},9:{}", (int) 'z', (int) 'Z', (int) '9');
        log.info("{}", new Solution().isPalindrome("0P"));
    }
}

/**
 * 一前 一后，对比所有的char，如果不等则就false，判定完就是回文
 */
class Solution {
    public boolean isPalindrome(String s) {
        for (int i = 0, j = s.length() - 1; i <= j; ) {
            char start = s.charAt(i);
            char end = s.charAt(j);
            if (!isAvaiChar(start)) {
                i++;
                continue;
            }
            if (!isAvaiChar(end)) {
                j--;
                continue;
            }
            if (!isEqual(start, end)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }

    private boolean isAvaiChar(char c) {
        if (c < '0' || c > 'z' || c > '9' && c < 'A' || c > 'Z' && c < 'a') {
            return false;
        }
        return true;
    }

    private boolean isEqual(char c1, char c2) {
        if (c1 >= '0' && c1 <= '9') {
            return c1 == c2;
        }
        return c1 == c2 || c1 == (char) (c2 + 32) || c1 == (char) (c2 - 32);
    }
}